<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Events;
use app\common\model\Goods;
use app\common\model\ScoreLog;
use app\common\model\sign\Daily;
use app\common\model\sign\User as UserSign;
use app\common\model\User;
use think\Db;

/**
 * 签到接口
 */
class Sign extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $weeks = [];
    protected $date = ('Y-m-d');

    public function _initialize()
    {
        parent::_initialize();

        $this->weeks = [
            ['day' => 1],
            ['day' => 2],
            ['day' => 3],
            ['day' => 4],
            ['day' => 5],
            ['day' => 6],
            ['day' => 7],
        ];
    }

    /**
     * @Author LK
     * @ApiTitle (签到配置)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function sign_confg()
    {
        $uid = $this->auth->id;
        $is_sign = false;
        foreach ($this->weeks as $k => $v) {
            $sign = UserSign::where(['user_id' => $uid])->whereTime('createtime', 'today')->count();
            if ($sign && $is_sign == false) {
                $is_sign = true;
            }
        }
        $week_count = UserSign::where(['user_id' => $uid])->whereTime('createtime', 'week')->count();

        $this->success('', [
            'list' => $this->weeks,
            'week_count' => $week_count,
            'is_sign' => $is_sign,
            'sign_point' =>  config('site.sign_point'),
            'consumption_point' => config('site.consumption_point'),
        ]);
    }


    /**
     * @Author LK
     * @ApiTitle (签到)
     * @ApiMethod (POST)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function sign()
    {
        $param = request()->param();
        extract($param);
        $uid = $this->auth->id;
        $date = date('Y-m-d');
        $yesterday = date("Y-m-d", strtotime("-1 day"));
        $yest = UserSign::get(['user_id' => $uid, 'signdate' => $yesterday]);
        $check = UserSign::get(['user_id' => $uid, 'signdate' => $date]);
        if ($check) {
            $this->error('今日已签到');
        }
        $sign_point = config('site.sign_point');
        Db::startTrans();
        try {
            $param['integral'] = $sign_point;
            $param['signdate'] = $date;
            $param['user_id'] = $uid;

            $res = UserSign::create($param, true);
            if ($res) {
                $user = User::get($uid);
                if ($yest) {
                    $user->days += 1;
                } else {
                    $user->days = 1;
                }
                $user->save();
                ScoreLog::income($uid, $sign_point, 1, 100, $res->id);
            }
            Db::commit();
        } catch (\Throwable $th) {

            Db::rollback();
            $this->error('签到失败');
        }
        $this->success('签到成功');
    }
}
